package com.polaris.live.adapter.party

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R.color
import com.polaris.live.R.drawable.party_game_seat
import com.polaris.live.databinding.AdapterPlayGameSeatItemBinding
import com.polaris.live.translation.R
import com.polaris.live.ui.party.widget.seat.bean.GameSeatBean
import com.polaris.live.widget.avatar.FrameImageUtils

/**
 * 游戏位adapter
 * @author Created by 天晴 on 2024/3/15 9:22
 * @since 1.0.0
 **/
class GameSeatAdapter : BaseQuickAdapter<GameSeatBean, GameSeatAdapter.GameSeatHolder>(0) {

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): GameSeatHolder {
        return GameSeatHolder(
            AdapterPlayGameSeatItemBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun convert(holder: GameSeatHolder, item: GameSeatBean) {
        holder.binding.apply {
            if (item.userBasicVo == null) {
                nameTv.text = StringUtils.getString(R.string.player_id_name, item.seatId + 1)
                avatarImg.setImageResource(party_game_seat)
                avFv.setFrameImage(null)
                nameTv.setTextColor(ColorUtils.getColor(color.white))
            } else {
                item.userBasicVo?.apply {
                    nameTv.text = nickname
                    nameTv.setNicknameColor(nicknameColor, color.white)
                    avatarImg.setImageUrl(avatar, userId)
                    avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(avatarFrame))
                }

            }
            if (item.isLoaded) {
                loadingBg.visibility = View.VISIBLE
                loadingHead.visibility = View.VISIBLE
            } else {
                loadingBg.visibility = View.GONE
                loadingHead.visibility = View.GONE
            }
        }
    }

    class GameSeatHolder(val binding: AdapterPlayGameSeatItemBinding) :
        BaseViewHolder(binding.root)
}